package com.keya.base.jwt;

import com.keya.base.exception.BusinessException;
import com.keya.config.JwtConfig;
import com.keya.config.OauthProperties;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Component
@Slf4j
public class JwtProvider {

    @Resource
    OauthProperties oauthProperties;

    @Resource
    JwtConfig jwtConfig;

    public String createToken(String client_id, String client_secret) {
        String sign = client_id + "_" + client_secret;
        Claims claims = Jwts.claims().setSubject(sign);
        Date now = new Date();
        Date validity = new Date(now.getTime() + 7 * 24 * 3600 * 1000);
        return Jwts.builder()
                .setClaims(claims)
                .setIssuedAt(now)
                .setExpiration(validity)
                .signWith(SignatureAlgorithm.HS512, sign)
                .compact();
    }

    public String createToken(String claimsContent, String client_id, String client_secret) {
        String sign = client_id + "_" + client_secret;
        Claims claims = Jwts.claims().setSubject(sign);
        claims.put("claimsContent", claimsContent);
        Date now = new Date();
        Date validity = new Date(now.getTime() + 7 * 24 * 3600 * 1000);
        return Jwts.builder()
                .setClaims(claims)
                .setIssuedAt(now)
                .setExpiration(validity)
                .signWith(SignatureAlgorithm.HS512, sign)
                .compact();
    }

    public boolean validateToken(String content, String token) throws BusinessException {
        Claims claims = parseJWT(token);
        String claimsContent = claims.get("claimsContent").toString();
        if (!claimsContent.contains(content)) {
            throw new BusinessException(40004, "token无效");
        }
        isExpiration(claims);
        return true;
    }

    public boolean validateToken(String token) throws BusinessException {
        Claims claims = parseJWT(token);
        isExpiration(claims);
        return true;
    }

    public boolean volatileFixationToken(String token) {
        return token.equals(jwtConfig.getFixationToken());
    }

    public void isExpiration(Claims claims) throws BusinessException {
        boolean before = claims.getExpiration().before(new Date());
        if (before) {
            throw new BusinessException(40004, "token超时");
        }
    }

    public Claims parseJWT(String token) throws BusinessException {
        try {
            String sign = oauthProperties.getClientId() + "_" + oauthProperties.getClientSecret();
            Jws<Claims> claimsJws = Jwts.parser().setSigningKey(sign).parseClaimsJws(token);
            return claimsJws.getBody();
        } catch (Exception e) {
            throw new BusinessException(40004, "token无效");
        }
    }


}
